﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class FormDisplay : Form
    {
        Thread updateData_T;
        int currentNumber = 0;
        public FormDisplay()
        {
            InitializeComponent();
        }

        private void FormDisplay_Load(object sender, EventArgs e)
        {
            //dgv_display.ColumnHeadersVisible = false;
            updateData_T = new Thread(UpdateData);
            updateData_T.IsBackground = true;
            updateData_T.Name = "UpdateData";
            updateData_T.Start();
        }

        private void Dgv_addRow(DataGridView dgv)
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new Action(() => {
                        if (dgv == null)
                        {
                            displalyInfo.displayInfoMain("要更新的dataGridView不能为空");
                            return;

                        }
                        DataGridViewRow dgvr = new DataGridViewRow();
                        DataGridViewTextBoxCell dgvc_index = new DataGridViewTextBoxCell();
                        dgvc_index.Value = GlobalVar.completeNum.ToString();
                        dgvr.Cells.Add(dgvc_index);

                        //尺寸测量更新
                        DataGridViewTextBoxCell dgvc_CenterUp = new DataGridViewTextBoxCell();
                        dgvc_CenterUp.Value = "OK";
                        dgvr.Cells.Add(dgvc_CenterUp);
                        DataGridViewTextBoxCell dgvc_CenterDown = new DataGridViewTextBoxCell();
                        dgvc_CenterDown.Value = GlobalVar.LastCircleTime.ToString();
                        dgvr.Cells.Add(dgvc_CenterDown);
                        //DataGridViewTextBoxCell dgvc_CenterLeft = new DataGridViewTextBoxCell();
                        //dgvc_CenterLeft.Value = GlobalVar.MeasureVisonResult[2].ToString();
                        //dgvr.Cells.Add(dgvc_CenterLeft);
                        DataGridViewTextBoxCell dgvc_CenterRight = new DataGridViewTextBoxCell();
                        dgvc_CenterRight.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dgvr.Cells.Add(dgvc_CenterRight);
                        //DataGridViewTextBoxCell dgvc_RightFPC = new DataGridViewTextBoxCell();
                        //dgvc_RightFPC.Value = GlobalVar.MeasureVisonResult[4].ToString();
                        //dgvr.Cells.Add(dgvc_RightFPC);

                        //DataGridViewTextBoxCell dgvc_CenterWeldX = new DataGridViewTextBoxCell();
                        //dgvc_CenterWeldX.Value = GlobalVar.FweldVisonResult[1].ToString();
                        //dgvr.Cells.Add(dgvc_CenterWeldX);
                        //DataGridViewTextBoxCell dgvc_CenterWeldY = new DataGridViewTextBoxCell();
                        //dgvc_CenterWeldY.Value = GlobalVar.FweldVisonResult[2].ToString();
                        //dgvr.Cells.Add(dgvc_CenterWeldY);
                        //DataGridViewTextBoxCell dgvc_EdgeWeldX = new DataGridViewTextBoxCell();
                        //dgvc_EdgeWeldX.Value = GlobalVar.LweldVisonResult[1].ToString();
                        //dgvr.Cells.Add(dgvc_EdgeWeldX);
                        //DataGridViewTextBoxCell dgvc_EdgeWeldY = new DataGridViewTextBoxCell();
                        //dgvc_EdgeWeldY.Value = GlobalVar.LweldVisonResult[2].ToString();
                        //dgvr.Cells.Add(dgvc_EdgeWeldY);

                        dgv.Rows.Add(dgvr);
                    }));
                }
                else
                {
                    if (dgv == null)
                    {
                        displalyInfo.displayInfoMain("要更新的dataGridView不能为空");
                        return;

                    }
                    DataGridViewRow dgvr = new DataGridViewRow();
                    DataGridViewTextBoxCell dgvc_index = new DataGridViewTextBoxCell();
                    dgvc_index.Value = GlobalVar.completeNum.ToString();
                    dgvr.Cells.Add(dgvc_index);

                    //尺寸测量更新
                    DataGridViewTextBoxCell dgvc_CenterUp = new DataGridViewTextBoxCell();
                    dgvc_CenterUp.Value = "OK";
                    dgvr.Cells.Add(dgvc_CenterUp);
                    DataGridViewTextBoxCell dgvc_CenterDown = new DataGridViewTextBoxCell();
                    dgvc_CenterDown.Value = GlobalVar.LastCircleTime.ToString();
                    dgvr.Cells.Add(dgvc_CenterDown);
                    //DataGridViewTextBoxCell dgvc_CenterLeft = new DataGridViewTextBoxCell();
                    //dgvc_CenterLeft.Value = GlobalVar.MeasureVisonResult[2].ToString();
                    //dgvr.Cells.Add(dgvc_CenterLeft);
                    DataGridViewTextBoxCell dgvc_CenterRight = new DataGridViewTextBoxCell();
                    dgvc_CenterRight.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    dgvr.Cells.Add(dgvc_CenterRight);
                    //DataGridViewTextBoxCell dgvc_RightFPC = new DataGridViewTextBoxCell();
                    //dgvc_RightFPC.Value = GlobalVar.MeasureVisonResult[4].ToString();
                    //dgvr.Cells.Add(dgvc_RightFPC);

                    //DataGridViewTextBoxCell dgvc_CenterWeldX = new DataGridViewTextBoxCell();
                    //dgvc_CenterWeldX.Value = GlobalVar.FweldVisonResult[1].ToString();
                    //dgvr.Cells.Add(dgvc_CenterWeldX);
                    //DataGridViewTextBoxCell dgvc_CenterWeldY = new DataGridViewTextBoxCell();
                    //dgvc_CenterWeldY.Value = GlobalVar.FweldVisonResult[2].ToString();
                    //dgvr.Cells.Add(dgvc_CenterWeldY);
                    //DataGridViewTextBoxCell dgvc_EdgeWeldX = new DataGridViewTextBoxCell();
                    //dgvc_EdgeWeldX.Value = GlobalVar.LweldVisonResult[1].ToString();
                    //dgvr.Cells.Add(dgvc_EdgeWeldX);
                    //DataGridViewTextBoxCell dgvc_EdgeWeldY = new DataGridViewTextBoxCell();
                    //dgvc_EdgeWeldY.Value = GlobalVar.LweldVisonResult[2].ToString();
                    //dgvr.Cells.Add(dgvc_EdgeWeldY);

                    dgv.Rows.Add(dgvr);
                }
               
            }
            catch (System.Exception ex)
            {
                displalyInfo.displayInfoMain("添加数据出错：" + ex.Message);
            }
        }

        private void writeCSV()
        {
            //StreamWriter
            StreamWriter streamWriter = new StreamWriter(GlobalVar.CSVpath, true, Encoding.Unicode);
            string value = string.Concat(new string[]
            {
                GlobalVar.completeNum.ToString(),
                "\t",
                "OK",
                "\t",
                 GlobalVar.LastCircleTime.ToString(),
                "\t",
                // GlobalVar.MeasureVisonResult[2].ToString(),
                //"\t",
                //GlobalVar.MeasureVisonResult[3].ToString(),
                //"\t",
                //GlobalVar.MeasureVisonResult[4].ToString(),
                //"\t",
                // GlobalVar.FweldVisonResult[1].ToString(),
                //"\t",
                // GlobalVar.FweldVisonResult[2].ToString(),
                //"\t",
                //GlobalVar.LweldVisonResult[1].ToString(),
                //"\t",
                //GlobalVar.LweldVisonResult[2].ToString(),
                //"\t",
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"),
                "\t\r\n"
            });
            streamWriter.Write(value);
            streamWriter.Flush();
            streamWriter.Close();
        }

        private void UpdateData()
        {
            while (true)
            {
                Thread.Sleep(50);
                if (currentNumber!=GlobalVar.completeNum)
                {
                    currentNumber = GlobalVar.completeNum;
                    GlobalVar.isNextCircle = true;
                    Dgv_addRow(dgv_display);
                    writeCSV();
                    if (InvokeRequired)
                    {
                        this.Invoke(new Action(() => {
                            dgv_display.Rows[dgv_display.Rows.Count - 1].Selected = true;
                            dgv_display.FirstDisplayedScrollingRowIndex = dgv_display.Rows.Count - 1;
                        }));
                    }
                    else
                    {
                        dgv_display.Rows[dgv_display.Rows.Count - 1].Selected = true;
                        dgv_display.FirstDisplayedScrollingRowIndex = dgv_display.Rows.Count - 1;
                    }
                }
                if (GlobalVar.isExit)
                {
                    break;
                }
            }
        }
    }
}
